package BookSearch;

import java.sql.SQLException;
import java.util.List;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import BookDB.BookBean;
import BookDB.h2DAO;
import Loginout.UserBean;

@WebServlet("/BasicSearch")
public class BasicSearch extends HttpServlet {
	private static final long serialVersionUID = 2L;

	
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = req.getSession();
		UserBean UB = (UserBean) session.getAttribute("ub");
	
		resp.setContentType("text/html;charset=UTF-8");
		req.setCharacterEncoding("UTF-8");
		PrintWriter writer = resp.getWriter();
		
		
		if (UB == null) {
			resp.sendRedirect("error.html");
		} else if (UB.vaild()) {
			try {
              
				String keyword=req.getParameter("keyword");
				if (keyword==null)
				{
					keyword="";
				}
				else
				{
				keyword = new String(req.getParameter("keyword").getBytes(
						"ISO-8859-1"), "utf-8");
				}
				System.out.print(keyword.toString());
				List<BookBean> BBlist = null;
				BBlist = h2DAO.SelectBookList(keyword);
			
				req.setAttribute("blist", BBlist);
				req.getRequestDispatcher("/SearchResult.jsp")
						.forward(req, resp);

			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
				writer.print("Search error!!");
			}
		}
	}
}
